/**
 * @author        Nicolas Bottarini <nicolasbottarini@gmail.com>, 404 <http://www.proyecto404.com>
 * @version       $Rev: 3 $
 * @lastrevision  $Date: 2011-09-12 16:45:37 +0000 (Mon, 12 Sep 2011) $	 
 */
package com.proyecto404.core.io {
	import com.proyecto404.core.BaseObject;
	import com.proyecto404.core.EventArgs;
	import com.proyecto404.core.log.fireflash.Console;
	
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import org.osflash.signals.ISignal;
	import org.osflash.signals.Signal;

	public class FileLoader extends BaseObject {
		private var _loadError:Signal;
		private var _progress:Signal;
		private var _loaded:Signal;
		
		public function FileLoader() {
			_loadError = new Signal(LoadErrorEventArgs);
			_progress = new Signal(FileLoadProgressEventArgs);
			_loaded = new Signal(FileLoadedEventArgs);
		}
		
		public function get loadError():ISignal {
			return _loadError;
		}
		
		public function get progress():ISignal {
			return _progress;
		}
		
		public function get loaded():ISignal {
			return _loaded;
		}
		
		public function load(file:String):void {
			var loader:URLLoader = new URLLoader();

			loader.addEventListener(IOErrorEvent.IO_ERROR, function(e:ErrorEvent):void {
				Console.debug("[FileLoader] no se ha podido cargar el archivo " + file);
				_loadError.dispatch(new LoadErrorEventArgs(file));
			});
			
			loader.addEventListener(ProgressEvent.PROGRESS, function(e:ProgressEvent):void {
				_progress.dispatch(new FileLoadProgressEventArgs(e.bytesLoaded, e.bytesTotal));
			});

			
			loader.addEventListener(Event.COMPLETE, function(e:Event):void {
				_loaded.dispatch(new FileLoadedEventArgs(file, new XML(e.target.data)));
			});
			loader.load(new URLRequest(file));
		}
	}
}